#coding=utf-8
import sys
import MySQLdb

reload(sys)
sys.setdefaultencoding('utf-8')

def getConn():
    conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="ahkimkoo",charset="utf8")
    return conn

def insertData():
    conn=getConn()
    cursor=conn.cursor()
    sql="insert into crawer(myid,ip,password,status)values(%s,%s,%s,%s)"
    #param=(1,'192.168.100.2','a123456',0);
    param=((1,"192.168.100.1","a123456",0),(1,"192.168.100.2","my password啊",0))
    cursor.executemany(sql,param)
    #cursor.execute(sql,param)
    conn.commit()
    cursor.close()
    conn.close()
    print "insert data finished"

def showData():
    conn=getConn()
    sql = "select * from crawer"
    cursor=conn.cursor()
    cursor.execute(sql)
    alldata = cursor.fetchall()
    if alldata:
       for rec in alldata:
           for item in rec:
               print "%-15s"%item,
           print
    cursor.close()
    conn.close()

def deleteAll():
    conn=getConn()
    cursor=conn.cursor()
    sql="delete from crawer"
    x=cursor.execute(sql)
    print "%d rows was deleted"%x
    conn.commit()
    cursor.close()
    conn.close()

   
if __name__ == "__main__":
    insertData()
    showData()
    deleteAll()